package com.peihua.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peihua.domain.Notification;
import com.peihua.mapper.NotificationMapper;
import com.peihua.service.NotificationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {
    @Override
    public Notification latestByType(String type) {
        return this.getOne(new LambdaQueryWrapper<Notification>()
                .eq(Notification::getType, type)
                .orderByDesc(Notification::getCreatedAt)
                .last("limit 1"));
    }

    @Override
    public List<Notification> latestByTypes(List<String> types) {
        List<Notification> result = new ArrayList<>();
        if (types == null || types.isEmpty()) return result;
        for (String t : types) {
            Notification n = latestByType(t);
            if (n != null) result.add(n);
        }
        return result;
    }
}